<?php

class Current_user {

    private static $user;
    private static $coockie_name;

    private function __construct() {
       self::$coockie_name = 'codeigniter_';
    }

    public static function auth() {
        $CI = & get_instance();
        $CI->load->library('session');
        if (!$user = Current_user::user()) {
            redirect('/');
        }
    }

    public static function auth_admin() {
        $CI = & get_instance();
        $CI->load->library('session');
        if (!$user = Current_user::user() and $user['level'] != 2) {
            redirect('/new_admin');
        }
    }

    public static function user() {

        if (!isset(self::$user)) {

            $CI = & get_instance();
            $CI->load->library('session');
            $CI->load->helper('cookie');

            if (get_cookie(self::$coockie_name.'_login') and get_cookie(self::$coockie_name.'_pass')) {
                if ($user_c = Doctrine::getTable('User')->findOneByemail(get_cookie(self::$coockie_name.'_login'))) {
                    if ($user_c['password'] == get_cookie(self::$coockie_name.'_pass')) {
                        $CI->session->set_userdata('user_id', $user_c['id']);
                        $CI->session->set_userdata('user_level', $user_c['level']);
                    }
                }
            }

            if (!$user_id = $CI->session->userdata('user_id')) {
                return FALSE;
            }

            if (!$u = Doctrine::getTable('User')->find($user_id)) {
                return FALSE;
            }

            $CI->session->set_userdata('user_id', $u->id);
            $CI->session->set_userdata('user_level', $u->level);

            $cookie = array(
                'name' => self::$coockie_name.'_login',
                'value' => $u->email,
                'expire' => '865000'
            );

            set_cookie($cookie);

            $cookie = array(
                'name' => self::$coockie_name.'_pass',
                'value' => $u->password,
                'expire' => '865000'
            );

            set_cookie($cookie);

            self::$user = $u;
        }

        return self::$user;
    }

    public static function exist($username) {
        if ($u = Doctrine::getTable('User')->findOneByemail($username)) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public static function login($username, $password) {

        // get User object by username
        if ($u = Doctrine::getTable('User')->findOneByemail($username)) {
            // this mutates (encrypts) the input password
            $u_input = new User();
            $u_input->password = $password;

            // password match (comparing encrypted passwords)
            if ($u->password == $u_input->password) {
                unset($u_input);

                $CI = & get_instance();
                $CI->load->library('session');
                $CI->session->set_userdata('user_id', $u->id);
                $CI->session->set_userdata('user_level', $u->level);
                self::$user = $u;

                return TRUE;
            }
            unset($u_input);
        }

        // login failed
        return FALSE;
    }

    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }

}
